program FairSquareCombinedSolutionAS;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  math,
  BigInt in 'BigInt.pas',
  BInt in 'BInt.pas';

var
  a, b: number;
  answer: Longint;

  FairSquaresListFile, IndexFile, InputFile, OutputFile: TextFile;
  IndexArray: array[1..100] of Integer;

  index, key, i, t: Integer;

  s: String;

procedure ProcessData;
var
  al: Byte;
  currentFairSquare: number;
  counter: Integer;
  n: longint;
begin
  al := len(a);
  CloseFile(FairSquaresListFile);
  Reset(FairSquaresListFile);
  for n := 1 to IndexArray[al] do
    Readln(FairSquaresListFile);
  counter := 0;
  repeat
    readFromFileOne(FairSquaresListFile, currentFairSquare);
  until (cmp(currentFairSquare, a) >= 0) or (eof(FairSquaresListFile));
  {if cmp(currentFairSquare, a) = 0 then
    counter := 1; }
  while not eof(FairSquaresListFile) and (cmp(currentFairSquare, b) <= 0) do begin
    readFromFileOne(FairSquaresListFile, currentFairSquare);
    inc(counter);
  end;
  answer := counter;

end;

begin
  try
    AssignFile(FairSquaresListFile, 'FirstAS.txt');
    Reset(FairSquaresListFile);
    AssignFile(IndexFile, 'IndexExpressAS.txt');
    Reset(IndexFile);

    s := ParamStr(1);
    if (s = '') then
       s := 'inputCombinedAS.txt';
    AssignFile(InputFile, s);
    Reset(InputFile);

    AssignFile(OutputFile, 'outputCombinedAS.txt');
    Rewrite(OutputFile);
    try
      while not eof(IndexFile) do begin
        Readln(IndexFile, index, key);
        IndexArray[index] := key;
      end;
      Readln(InputFile, t);
      for i := 1 to t do begin
        answer := 0;
        readFromFile(InputFile, a, b);
        ProcessData;
        writeln(OutputFile, 'Case #', i, ': ', answer);

        writeln('Case #', i, ': ', answer);
      end;
      writeln('Finished');
      Readln;
    finally
      CloseFile(FairSquaresListFile);
      CloseFile(IndexFile);
      CloseFile(OutputFile);
    end;
    { TODO -oUser -cConsole Main : Insert code here }
  except
    on E: Exception do begin
      Writeln(E.ClassName, ': ', E.Message);
      readln;
    end;
  end;
end.
